package com.example.studyktdemo.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.studyktdemo.bean.CateGoryResponse
import com.example.studyktdemo.interfaces.RetrofitApi
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class CategoryViewModel:ViewModel() {

    var categoryMutableLiveData = MutableLiveData<CateGoryResponse>()

    fun queryCategoryList(){
        val retrofit = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl("https://baobab.kaiyanapp.com/api/")
            .build()
        val request: RetrofitApi = retrofit.create(RetrofitApi::class.java)
        val call: Call<CateGoryResponse> = request.categories
        call.enqueue(object : Callback<CateGoryResponse> {
            override fun onResponse(call: Call<CateGoryResponse>, response: Response<CateGoryResponse>) {
                if (response.body() != null) {
                    categoryMutableLiveData.postValue(response.body())
                }
            }

            override fun onFailure(call: Call<CateGoryResponse>, e: Throwable) {
                categoryMutableLiveData.postValue(null)
            }

        })

    }
}